package com.java.auth.process;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程变量的使用方法
 */
@SpringBootTest
public class processTest03 {

    //该服务负责部署流程
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    /////////////////////////////////
    @Test
    public void deployProcess03() {
        // 流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban.bpmn20.xml")
                .name("加班申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
    //启动流程实例
    @Test
    public void startProcessInstance(){
        Map<String,Object> map = new HashMap<>();
        map.put("assignee1","kk");
        //map.put("assignee2","mike");
        //创建流程实例,我们需要知道流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban", map);
        System.out.println(processInstance.getProcessDefinitionId()+" 流程定义id");
        System.out.println(processInstance.getId()+" 流程实例id");
    }
    //3、查询任务
    @Test
    public void findTask(){

        List<Task> list = taskService.createTaskQuery().taskAssignee("ll").list();//只查询该任务负责人的任务
        for (Task task: list) {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }

    }
    //4、完成任务
    @Test
    public void completeTask(){
        //返回一条任务
        Task task = taskService.createTaskQuery().taskAssignee("ll").singleResult();

        //在任务办理时设置流程变量
//        Map<String,Object> map = new HashMap<>();
//        map.put("assignee2","ll");
        //完成任务参数是任务id
//        taskService.complete(task.getId(),map);
        taskService.complete(task.getId());
    }

}
